'use strict';

const Controller = require('egg').Controller;

class SpeedProfileController extends Controller {
  async list() {
     /**
        * @summary speedProfilelist。
        * @description speedProfilelist。
        * @router get /speedprofilelist
        * @request query integer limit
        * @request query integer page
        * @request query String OltId
        * @request query String search
        */
      const params = this.ctx.query;
      const result = await this.ctx.service.speedProfile.list(params);
      this.ctx.body = {
        msg: '',
        status: 200,
        data: result.rows,
        count: result.count,
      };
  }
  async allList() {
    /**
        * @summary speedProfilealllist。
        * @description speedProfilealllist。
        * @router get /speedprofilealllist
        * @request query String OltId
        */
      const params = this.ctx.query; 
      const result = await this.ctx.service.speedProfile.allList(params);
      this.ctx.body = {
        msg: '',
        status: 200,
        data: result,
      };
  }
  async create() {
    const params = this.ctx.request.body;
    let oltResult = await this.ctx.model.Olt.findOne({_id:params.oltId})
    const result = await this.ctx.service.speedProfile.create(params);
    global.log.desc = ` ${oltResult.name} ${oltResult.oltIP} `+result.msg
    global.log.after = `Name:${params.name},description:${params.description},CBS:${params.CBS},PBS:${params.PBS},SIR:${params.SIR},PIR:${params.PIR}`
    this.ctx.body = {
      status:200,
      data:result
    }
  }
  async delete() {
    const params = this.ctx.query
    let speedProfileResult = await this.ctx.model.SpeedProfile.findOne({_id:params.id})
    let oltResult = await this.ctx.model.Olt.findOne({_id:speedProfileResult.oltId})
    const result = await this.ctx.service.speedProfile.delete(params)
    global.log.desc = ` ${oltResult.name} ${oltResult.oltIP} `+result.msg
    this.ctx.body = {
      status:200,
      data:result 
    }
  }
  async edit() {
    const params = this.ctx.request.body;
    let oltResult = await this.ctx.model.Olt.findOne({_id:params.oltId})
    const result = await this.ctx.service.speedProfile.edit(params)
    global.log.desc = ` ${oltResult.name} ${oltResult.oltIP} `+result.msg
    this.ctx.body = {
      status:200,
      data:result
    }
  }
}

module.exports = SpeedProfileController;
